﻿// StreamingContextExtensions.cs
//
// Copyright 2008 by Burt Harris
// Licensed under the Microsoft Public License (Ms-PL)
// See: http://www.codeplex.com/yaml/license
//
using System;
using System.Runtime.Serialization;

namespace YaTools.Yaml.Serialization
{
    [Obsolete("Replace with Emitter/Mode/Waypoint")]
    public static class StreamingContextExtensions
    {

        public static OldYamlWriter YamlWriter(this StreamingContext context)
        {
            OldYamlWriter writer = context.Context as OldYamlWriter;
            if (writer == null)         // Try YamlContext
            {
                YamlContext c2 = context.Context as YamlContext;
                if (c2 != null)
                    writer = c2.Writer as OldYamlWriter;
            }
            if (writer == null)         // Try IServiceProvider
            {
                IServiceProvider sp = context.Context as IServiceProvider;
                if (sp != null)
                    writer = sp.GetService(Globals.TypeOfYamlWriter) as OldYamlWriter;
            }
            if (writer == null)
                throw new ArgumentException("Unable to convert context to YamlWriter");
            return writer;
        }

        public static YamlTextWriter YamlTextWriter(this StreamingContext context)
        {
            YamlTextWriter writer = context.Context as YamlTextWriter;
            if (writer == null)         // Try YamlContext
            {
                YamlContext c2 = context.Context as YamlContext;
                if (c2 != null)
                    writer = c2.Writer as YamlTextWriter;
            }
            if (writer == null)         // Try IServiceProvider
            {
                IServiceProvider sp = context.Context as IServiceProvider;
                if (sp != null)
                    writer = sp.GetService(Globals.TypeOfYamlTextWriter) as YamlTextWriter;
            }
            if (writer == null)
                throw new ArgumentException("Unable to convert context to YamlTextWriter");
            return writer;
        }
    }
}
